source("libraries.R")

##########################################
########################################## 
## Construct Figure 2 of Main Text
##########################################
########################################## 
load(file=here("Data_recoded","fig2_left.R"))
load(file=here("Data_recoded","fig2_right.R"))

ggarrange(fig2_left,fig2_right,
          ncol = 2, nrow = 1)

ggsave(here::here("figures", "fig2.pdf"),
       width = 10, height = 4)

##########################################
########################################## 
## Construct Figure 3 of Main Text
##########################################
########################################## 
###load figures
load(file=here("Data_recoded","study1_fig3.R"))
load(file=here("Data_recoded","study2_fig3.R"))
load(file=here("Data_recoded","study3_fig3.R"))
load(file=here("Data_recoded","study4_fig3.R"))
load(file=here("Data_recoded","study5_fig3.R"))
load(file=here("Data_recoded","study6_fig3.R"))

### Need for Chaos
all <- rbind(study1_coef,study2_coef,study3_coef,study4_coef,study5_coef,study6_coef) %>% 
  mutate(vars_names = case_when(
    term != "repdem" ~ "Need for Chaos",
    TRUE ~ "Partisanship"
  )) %>% 
  mutate(true_share = case_when(
    dv == "share_dem" | dv == "share_rep" ~ "Intentions to Share Rumors",
    TRUE ~ "Believing Rumors are True"
  )) %>% 
  mutate(`Rumor Target` = case_when(
    dv == "share_dem" | dv == "true_dem" ~ "Democrats",
    TRUE ~ "Republicans"
  )) %>% 
  select(-term)

all$new_name <- paste0(all$vars_names," (" , all$study,")")

fig3_nfc_dat <- all %>% 
  filter(vars_names == "Need for Chaos") %>% 
  mutate(labels_dem = case_when(
    dv == "share_dem" & study == "study6" ~ "Democratic Rumors",
    dv == "true_dem" & study == "study6" ~ "Democratic Rumors",    
    TRUE ~ NA_character_
  )) %>% 
  mutate(labels_rep = case_when(
    dv == "share_rep" & study == "study6" ~ "Republican Rumors",
    dv == "true_rep" & study == "study6" ~ "Republican Rumors",    
    TRUE ~ NA_character_
  ))  

nfc_plot <-  ggplot(fig3_nfc_dat) + 
  coord_flip() +
  geom_hline(yintercept=0, color="black", size = .5, alpha = .8) + 
  geom_point(aes(x = new_name , y = estimate, shape = `Rumor Target`, 
                 color = `Rumor Target`, 
                 fill = `Rumor Target`), size = 2.2 ,
             position = position_dodge(.65)) +
  geom_linerange(aes(x = new_name, y = estimate, ymax = estimate+2*std.error, 
                     ymin = estimate-2*std.error, color = `Rumor Target`),
                 position = position_dodge(.65),
                 size =1.2,
                 alpha = .8) +
  facet_wrap(~true_share) +
  geom_text(aes(x = new_name, y = estimate, label = labels_dem, color = `Rumor Target`), nudge_y = -0.01, nudge_x = -.35 , size=2.5, fontface = "bold") +
  geom_text(aes(x = new_name, y = estimate,label = labels_rep, color = `Rumor Target`), nudge_y = 0.01, nudge_x = .37 , size=2.5, fontface = "bold") +
  scale_color_manual(values=c("black", "azure4")) +
  theme_bw() +
  theme(
    legend.position = "none",
    axis.text.x = element_text(size = 10)
  ) +
  scale_y_continuous(breaks = c(-.1,-.05,0,.05,.1,.15,.2,.25,.3), 
                     labels = c("-.1","","0","",".1","",".2","",".3"),
                     limits = c(-.1,.3)) +
  scale_x_discrete("") + 
  ylab("")

### Partisanship
pid_dat <- all %>% 
  filter(vars_names == "Partisanship") %>% 
  mutate(labels_dem = case_when(
    dv == "share_dem" & study == "study6" ~ "Democratic Rumors",
    dv == "true_dem" & study == "study6" ~ "Democratic Rumors",    
    TRUE ~ NA_character_
  )) %>% 
  mutate(labels_rep = case_when(
    dv == "share_rep" & study == "study6" ~ "Republican Rumors",
    dv == "true_rep" & study == "study6" ~ "Republican Rumors",    
    TRUE ~ NA_character_
  ))  

pid_plot <- ggplot(pid_dat) + 
  coord_flip() +
  geom_hline(yintercept=0, color="black", size = .5, alpha = .8) + 
  geom_point(aes(x = new_name , y = estimate, shape = `Rumor Target`, 
                 color = `Rumor Target`, 
                 fill = `Rumor Target`), size = 2.2 ,
             position = position_dodge(.65)) +
  geom_linerange(aes(x = new_name, y = estimate, ymax = estimate+2*std.error, 
                     ymin = estimate-2*std.error, color = `Rumor Target`),
                 position = position_dodge(.65),
                 size =1.2,
                 alpha = .8) +
  facet_wrap(~true_share) +
  geom_text(aes(x = new_name, y = estimate, label = labels_dem, color = `Rumor Target`), nudge_y = -0.01, nudge_x = -.35 , size=2.5, fontface = "bold") +
  geom_text(aes(x = new_name, y = estimate,label = labels_rep, color = `Rumor Target`), nudge_y = 0.01, nudge_x = .37 , size=2.5, fontface = "bold") +
  scale_color_manual(values=c("black", "azure4")) +
  theme_bw() +
  theme(
    legend.position = "none",
    axis.text.x = element_text(size = 10)
  ) +
  scale_y_continuous(breaks = c(-.6,-.5,-.4,-.3,-.2,-.1,0,.1,.2,.3), 
                     labels = c("-.6","","-.4","","-.2","","0","",".2",""),
                     limits = c(-.6,.3)) +
  scale_x_discrete("") + 
  ylab("")

ggarrange(nfc_plot,pid_plot,
          ncol = 1, nrow = 2)

ggsave(here::here("figures", "fig3.pdf"),
       width = 6, height = 6)

##########################################
########################################## 
## Construct Figure 4 of Main Text
##########################################
########################################## 
###load figures for Dem participants
load(file=here("Data_recoded","study1_fig4_dem.R"))
load(file=here("Data_recoded","study2_fig4_dem.R"))
load(file=here("Data_recoded","study3_fig4_dem.R"))
load(file=here("Data_recoded","study4_fig4_dem.R"))
load(file=here("Data_recoded","study6_fig4_dem.R"))

tog <- rbind(study1_coef_dem,study2_coef_dem,study3_coef_dem,study4_coef_dem,study6_coef_dem) %>% 
  mutate(true_share = case_when(
    dv == "share_dem" | dv == "share_rep" ~ "Intentions to Share Rumors",
    TRUE ~ "Believing Rumors are True"
  )) %>% 
  mutate(`Rumor Target` = case_when(
    dv == "share_dem" | dv == "true_dem" ~ "Democrats",
    TRUE ~ "Republicans"
  )) %>% 
  select(-term)

tog$new_name <- paste0("Need for Chaos"," (" , tog$study,")")


nfc <- tog %>% 
  mutate(labels_dem = case_when(
    dv == "share_dem" & study == "study6" ~ "Democratic Rumors",
    dv == "true_dem" & study == "study6" ~ "Democratic Rumors",    
    TRUE ~ NA_character_
  )) %>% 
  mutate(labels_rep = case_when(
    dv == "share_rep" & study == "study6" ~ "Republican Rumors",
    dv == "true_rep" & study == "study6" ~ "Republican Rumors",    
    TRUE ~ NA_character_
  ))  


nfc_plot_dem <-  ggplot(nfc) + 
  coord_flip() +
  geom_hline(yintercept=0, color="black", size = .5, alpha = .8) + 
  geom_point(aes(x = new_name , y = estimate, shape = `Rumor Target`, 
                 color = `Rumor Target`, 
                 fill = `Rumor Target`), size = 2.2 ,
             position = position_dodge(.65)) +
  geom_linerange(aes(x = new_name, y = estimate, ymax = estimate+2*std.error, 
                     ymin = estimate-2*std.error, color = `Rumor Target`),
                 position = position_dodge(.65),
                 size =1.2,
                 alpha = .8) +
  facet_wrap(~true_share) +
  geom_text(aes(x = new_name, y = estimate, label = labels_dem, color = `Rumor Target`), nudge_y = -0.01, nudge_x = -.35 , size=2.5, fontface = "bold") +
  geom_text(aes(x = new_name, y = estimate,label = labels_rep, color = `Rumor Target`), nudge_y = 0.01, nudge_x = .37 , size=2.5, fontface = "bold") +
  scale_color_manual(values=c("black", "azure4")) +
  theme_bw() +
  theme(
    legend.position = "none",
    axis.text.x = element_text(size = 10)
  ) +
  scale_y_continuous(breaks = c(-.1,-.05,0,.05,.1,.15,.2,.25,.3), 
                     labels = c("-.1","","0","",".1","",".2","",".3"),
                     limits = c(-.1,.3)) +
  scale_x_discrete("") + 
  ylab("") +
ggtitle("Democratic Identifiers")

###load figures for Rep participants
load(file=here("Data_recoded","study1_fig4_rep.R"))
load(file=here("Data_recoded","study2_fig4_rep.R"))
load(file=here("Data_recoded","study3_fig4_rep.R"))
load(file=here("Data_recoded","study4_fig4_rep.R"))
load(file=here("Data_recoded","study6_fig4_rep.R"))

tog <- rbind(study1_coef_rep,study2_coef_rep,study3_coef_rep,study4_coef_rep,study6_coef_rep) %>% 
  mutate(true_share = case_when(
    dv == "share_dem" | dv == "share_rep" ~ "Intentions to Share Rumors",
    TRUE ~ "Believing Rumors are True"
  )) %>% 
  mutate(`Rumor Target` = case_when(
    dv == "share_dem" | dv == "true_dem" ~ "Democrats",
    TRUE ~ "Republicans"
  )) %>% 
  select(-term)


tog$new_name <- paste0("Need for Chaos"," (" , tog$study,")")


nfc <- tog %>% 
  mutate(labels_dem = case_when(
    dv == "share_dem" & study == "study6" ~ "Democratic Rumors",
    dv == "true_dem" & study == "study6" ~ "Democratic Rumors",    
    TRUE ~ NA_character_
  )) %>% 
  mutate(labels_rep = case_when(
    dv == "share_rep" & study == "study6" ~ "Republican Rumors",
    dv == "true_rep" & study == "study6" ~ "Republican Rumors",    
    TRUE ~ NA_character_
  ))  


nfc_plot_rep <-  ggplot(nfc) + 
  coord_flip() +
  geom_hline(yintercept=0, color="black", size = .5, alpha = .8) + 
  geom_point(aes(x = new_name , y = estimate, shape = `Rumor Target`, 
                 color = `Rumor Target`, 
                 fill = `Rumor Target`), size = 2.2 ,
             position = position_dodge(.65)) +
  geom_linerange(aes(x = new_name, y = estimate, ymax = estimate+2*std.error, 
                     ymin = estimate-2*std.error, color = `Rumor Target`),
                 position = position_dodge(.65),
                 size =1.2,
                 alpha = .8) +
  facet_wrap(~true_share) +
  geom_text(aes(x = new_name, y = estimate, label = labels_dem, color = `Rumor Target`), nudge_y = -0.01, nudge_x = -.35 , size=2.5, fontface = "bold") +
  geom_text(aes(x = new_name, y = estimate,label = labels_rep, color = `Rumor Target`), nudge_y = 0.01, nudge_x = .37 , size=2.5, fontface = "bold") +
  scale_color_manual(values=c("black", "azure4")) +
  theme_bw() +
  theme(
    legend.position = "none",
    axis.text.x = element_text(size = 10)
  ) +
  scale_y_continuous(breaks = c(-.1,-.05,0,.05,.1,.15,.2,.25,.3), 
                     labels = c("-.1","","0","",".1","",".2","",".3"),
                     limits = c(-.1,.3)) +
  scale_x_discrete("") + 
  ylab("") +
  ggtitle("Republican Identifiers")

ggarrange(nfc_plot_dem,nfc_plot_rep,
          ncol = 1, nrow = 2)

ggsave(here::here("figures", "fig4.pdf"),
       width = 6, height = 6)


##########################################
########################################## 
## Construct Figure S2 of Supplementary Materials
##########################################
########################################## 
load(file=here("Data_recoded","study1_figS2.R"))
load(file=here("Data_recoded","study2_figS2.R"))
load(file=here("Data_recoded","study3_figS2.R"))
load(file=here("Data_recoded","study4_figS2.R"))
load(file=here("Data_recoded","study6_figS2.R"))

###
tog <- rbind(study1_coef_independents,study2_coef_independents,study3_coef_independents,study4_coef_independents,study6_coef_independents) %>% 
  mutate(true_share = case_when(
    dv == "share_dem" | dv == "share_rep" ~ "Intentions to Share Rumors",
    TRUE ~ "Believing Rumors are True"
  )) %>% 
  mutate(`Rumor Target` = case_when(
    dv == "share_dem" | dv == "true_dem" ~ "Democrats",
    TRUE ~ "Republicans"
  )) %>% 
  select(-term)


tog <- tog %>% 
  mutate(study = case_when(
    study == "study1" ~ "study1",
    study == "study2" ~ "study3",
    study == "study3" ~ "study2",
    study == "study4" ~ "study5",
    study == "study6" ~ "study6",
    TRUE ~ NA_character_
  ))

tog$new_name <- paste0("Need for Chaos"," (" , tog$study,")")

nfc <- tog %>% 
  mutate(labels_dem = case_when(
    dv == "share_dem" & study == "study6" ~ "Democratic Rumors",
    dv == "true_dem" & study == "study6" ~ "Democratic Rumors",    
    TRUE ~ NA_character_
  )) %>% 
  mutate(labels_rep = case_when(
    dv == "share_rep" & study == "study6" ~ "Republican Rumors",
    dv == "true_rep" & study == "study6" ~ "Republican Rumors",    
    TRUE ~ NA_character_
  ))  


nfc_plot_independents <- ggplot(nfc, aes(x = new_name,
                                y = estimate,
                                fill = `Rumor Target`, color = `Rumor Target`)) +
  geom_hline(yintercept=0, color="gray", size = .5, alpha = .8) + 
  geom_linerange(aes(ymax = estimate+2*std.error, 
                     ymin = estimate-2*std.error),
                 position = position_dodge(.5),
                 size =1.5,
                 alpha = .4) +
  geom_point(size = 2.8,
             position = position_dodge(.5),
             shape = 19) + 
  geom_text(aes(label = labels_dem), nudge_y = 0.00, nudge_x = -.35 , size=2.5, fontface = "bold") +
  geom_text(aes(label = labels_rep), nudge_y = 0.00, nudge_x = .35 , size=2.4, fontface = "bold") +
  scale_x_discrete("") + 
  ylab("") +
  ylim(-.2,.3) +
  coord_flip() + 
  facet_wrap(~true_share) +
  scale_color_manual(values=c("black", "azure4")) +
  theme_bw() +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    legend.position = "none"
  ) + ggtitle("Need for Chaos Among Independents")


nfc_plot_independents

ggsave(here::here("figures", "figS2.pdf"),
       width = 7, height = 5, dpi = 700)




